<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    

    

    



    <meta charset="utf-8">
    
    
    <meta name="sogou_site_verification" content="true">
    
    
    
    <title>Git命令学习 | Lvshen&#39;s Blog | This is My World</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="Git,命令">
    <meta name="baidu-site-verification" content="VIVNdSiMZm">
    <meta name="description" content="Git重要概念master head每次提交，Git都把它们串成一条时间线，这条时间线就是一个分支。在Git里，有个分支叫主分支，即master分支。HEAD严格来说不是指向提交，而是指向master，master才是指向提交的，所以，HEAD指向的就是当前分支。 一开始的时候，master分支是一条线，Git用master指向最新的提交，再用HEAD指向master，就能确定当前分支，以及当前分">
<meta name="keywords" content="Git,命令">
<meta property="og:type" content="article">
<meta property="og:title" content="Git命令学习">
<meta property="og:url" content="https://lvshen9.gitee.io/2018/07/06/1/index.html">
<meta property="og:site_name" content="Lvshen&#39;s Blog">
<meta property="og:description" content="Git重要概念master head每次提交，Git都把它们串成一条时间线，这条时间线就是一个分支。在Git里，有个分支叫主分支，即master分支。HEAD严格来说不是指向提交，而是指向master，master才是指向提交的，所以，HEAD指向的就是当前分支。 一开始的时候，master分支是一条线，Git用master指向最新的提交，再用HEAD指向master，就能确定当前分支，以及当前分">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://s2.ax1x.com/2019/01/17/kpljRH.png">
<meta property="og:image" content="https://s2.ax1x.com/2019/01/17/kplzQA.png">
<meta property="og:image" content="https://s2.ax1x.com/2019/01/17/kplvzd.png">
<meta property="og:image" content="https://s2.ax1x.com/2019/01/17/kp1CeP.png">
<meta property="og:image" content="https://s2.ax1x.com/2019/01/17/kp1pLt.png">
<meta property="og:image" content="https://s2.ax1x.com/2019/01/17/kp1Pdf.png">
<meta property="og:image" content="https://s2.ax1x.com/2019/01/17/kp1SsI.png">
<meta property="og:updated_time" content="2019-01-17T06:58:14.112Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Git命令学习">
<meta name="twitter:description" content="Git重要概念master head每次提交，Git都把它们串成一条时间线，这条时间线就是一个分支。在Git里，有个分支叫主分支，即master分支。HEAD严格来说不是指向提交，而是指向master，master才是指向提交的，所以，HEAD指向的就是当前分支。 一开始的时候，master分支是一条线，Git用master指向最新的提交，再用HEAD指向master，就能确定当前分支，以及当前分">
<meta name="twitter:image" content="https://s2.ax1x.com/2019/01/17/kpljRH.png">
    
    <link rel="shortcut icon" href="/img/mylogo.jpg">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">我的技术小房间</h5>
          <a href="mailto:https://lvshen9.github.io" title="https://lvshen9.github.io" class="mail">https://lvshen9.github.io</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/about"  >
                <i class="icon icon-lg icon-address-book"></i>
                About
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/collection"  >
                <i class="icon icon-lg icon-apple"></i>
                Collection
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://lvshen9.github.io/" target="_blank" >
                <i class="icon icon-lg icon-wordpress"></i>
                Blog
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/lvshen9" target="_blank" >
                <i class="icon icon-lg icon-github-alt"></i>
                GitHub
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">Git命令学习</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">Git命令学习</h1>
        <h5 class="subtitle">
            
                <time datetime="2018-07-06T06:45:41.000Z" itemprop="datePublished" class="page-time">
  2018-07-06
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/技术/">技术</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Git重要概念"><span class="post-toc-number">1.</span> <span class="post-toc-text">Git重要概念</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#master-head"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">master head</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#工作区，暂存区"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">工作区，暂存区</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#下载安装"><span class="post-toc-number">2.</span> <span class="post-toc-text">下载安装</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#初始化"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">初始化</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-6"><a class="post-toc-link" href="#初始化参数"><span class="post-toc-number">2.1.1.</span> <span class="post-toc-text">初始化参数</span></a></li><li class="post-toc-item post-toc-level-6"><a class="post-toc-link" href="#初始化本地仓库"><span class="post-toc-number">2.1.2.</span> <span class="post-toc-text">初始化本地仓库</span></a></li><li class="post-toc-item post-toc-level-6"><a class="post-toc-link" href="#SSH-key生成"><span class="post-toc-number">2.1.3.</span> <span class="post-toc-text">SSH key生成</span></a></li><li class="post-toc-item post-toc-level-6"><a class="post-toc-link" href="#clone代码"><span class="post-toc-number">2.1.4.</span> <span class="post-toc-text">clone代码</span></a></li><li class="post-toc-item post-toc-level-6"><a class="post-toc-link" href="#gitignore生效办法"><span class="post-toc-number">2.1.5.</span> <span class="post-toc-text">.gitignore生效办法</span></a></li><li class="post-toc-item post-toc-level-6"><a class="post-toc-link" href="#查看各种状态"><span class="post-toc-number">2.1.6.</span> <span class="post-toc-text">查看各种状态</span></a></li><li class="post-toc-item post-toc-level-6"><a class="post-toc-link" href="#分支"><span class="post-toc-number">2.1.7.</span> <span class="post-toc-text">分支</span></a></li></ol></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#更新和提交代码"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">更新和提交代码</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#撤销"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">撤销</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#规则"><span class="post-toc-number">2.4.</span> <span class="post-toc-text">规则</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#特殊情况"><span class="post-toc-number">2.5.</span> <span class="post-toc-text">特殊情况</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#特别提示"><span class="post-toc-number">2.6.</span> <span class="post-toc-text">特别提示</span></a></li></ol></li></ol>
        </nav>
    </aside>


<article id="post-1"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">Git命令学习</h1>
        <div class="post-meta">
            <time class="post-time" title="2018-07-06 14:45:41" datetime="2018-07-06T06:45:41.000Z"  itemprop="datePublished">2018-07-06</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/技术/">技术</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h4 id="Git重要概念"><a href="#Git重要概念" class="headerlink" title="Git重要概念"></a>Git重要概念</h4><h5 id="master-head"><a href="#master-head" class="headerlink" title="master head"></a>master head</h5><p>每次提交，Git都把它们串成一条时间线，这条时间线就是一个分支。在Git里，有个分支叫主分支，即master分支。HEAD严格来说不是指向提交，而是指向master，master才是指向提交的，所以，HEAD指向的就是当前分支。</p>
<p>一开始的时候，master分支是一条线，Git用master指向最新的提交，再用HEAD指向master，就能确定当前分支，以及当前分支的提交点：</p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="https://s2.ax1x.com/2019/01/17/kpljRH.png" alt title>
                </div>
                <div class="image-caption"></div>
            </figure>
<a id="more"></a>
<p>每次提交，master分支都会向前移动一步，这样，随着你不断提交，master分支的线也越来越长：当我们创建新的分支，例如dev时，Git新建了一个指针叫dev，指向master相同的提交，再把HEAD指向dev，就表示当前分支在dev上：</p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="https://s2.ax1x.com/2019/01/17/kplzQA.png" alt title>
                </div>
                <div class="image-caption"></div>
            </figure>
<p>从现在开始，对工作区的修改和提交就是针对dev分支了，比如新提交一次后，dev指针往前移动一步，而master指针不变：</p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="https://s2.ax1x.com/2019/01/17/kplvzd.png" alt title>
                </div>
                <div class="image-caption"></div>
            </figure>
<p>假如我们在dev上的工作完成了，就可以把dev合并到master上。Git怎么合并呢？最简单的方法，就是直接把master指向dev的当前提交，就完成了合并：</p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="https://s2.ax1x.com/2019/01/17/kp1CeP.png" alt title>
                </div>
                <div class="image-caption"></div>
            </figure>
<p>合并完分支后，甚至可以删除dev分支。删除dev分支就是把dev指针给删掉，删掉后，我们就剩下了一条master分支：</p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="https://s2.ax1x.com/2019/01/17/kp1pLt.png" alt title>
                </div>
                <div class="image-caption"></div>
            </figure>
<h5 id="工作区，暂存区"><a href="#工作区，暂存区" class="headerlink" title="工作区，暂存区"></a>工作区，暂存区</h5><ul>
<li>工作区Workspace：就是你在电脑里能看到的目录，即你代码放的那个文件夹。即时性强，对文件的所有更改都会立刻提现在这里。</li>
<li>版本库：工作区有一个隐藏目录.git，这个不算工作区，而是Git的版本库。</li>
<li>暂存区 Index / Stage：git add以后，当前对文件的更改会保存到这个区</li>
<li>本地仓库Repository：git commit以后，当前暂存区里对文件的更改会提交到本地仓库</li>
<li>远程仓库Remote：远程仓库名一般叫origin。git push以后，本地仓库里优先于远程仓库的commit会被push到远程仓库</li>
</ul>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="https://s2.ax1x.com/2019/01/17/kp1Pdf.png" alt title>
                </div>
                <div class="image-caption"></div>
            </figure>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="https://s2.ax1x.com/2019/01/17/kp1SsI.png" alt title>
                </div>
                <div class="image-caption"></div>
            </figure>
<h4 id="下载安装"><a href="#下载安装" class="headerlink" title="下载安装"></a>下载安装</h4><p><a href="https:#git-scm.com/download/" target="_blank" rel="noopener">git官网下载</a></p>
<h5 id="初始化"><a href="#初始化" class="headerlink" title="初始化"></a>初始化</h5><h6 id="初始化参数"><a href="#初始化参数" class="headerlink" title="初始化参数"></a>初始化参数</h6><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> git config --global user.name <span class="string">"你的名字"</span></span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git config --global user.email <span class="string">"你的邮箱地址"</span></span></span><br></pre></td></tr></table></figure>
<p>因为Git是分布式版本控制系统，所以，每个机器都必须自报家门：你的名字和Email地址。<br> 注意git config命令的–global参数，用了这个参数，表示你这台机器上所有的Git仓库都会使用这个配置，当然也可以对某个仓库指定不同的用户名和Email地址。</p>
<h6 id="初始化本地仓库"><a href="#初始化本地仓库" class="headerlink" title="初始化本地仓库"></a>初始化本地仓库</h6><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> git init</span></span><br></pre></td></tr></table></figure>
<h6 id="SSH-key生成"><a href="#SSH-key生成" class="headerlink" title="SSH key生成"></a>SSH key生成</h6><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> ssh-keygen -t rsa -C <span class="string">"你的邮箱地址"</span></span></span><br></pre></td></tr></table></figure>
<h6 id="clone代码"><a href="#clone代码" class="headerlink" title="clone代码"></a>clone代码</h6><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 克隆master分支</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git <span class="built_in">clone</span> &lt;版本库的网址&gt;</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 指定克隆的分支名</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git <span class="built_in">clone</span> -b &lt;分支名&gt; &lt;版本库的网址&gt;</span></span><br></pre></td></tr></table></figure>
<h6 id="gitignore生效办法"><a href="#gitignore生效办法" class="headerlink" title=".gitignore生效办法"></a>.gitignore生效办法</h6><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 先把本地缓存删除（改变成未track状态）</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git rm -r --cached .</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 然后再提交</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git add .</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git commit -m <span class="string">'update .gitignore'</span></span></span><br></pre></td></tr></table></figure>
<h6 id="查看各种状态"><a href="#查看各种状态" class="headerlink" title="查看各种状态"></a>查看各种状态</h6><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 查看当前状态（分支名，有哪些改动，有哪些冲突，工作区暂存区中的内容，几个commit等等）</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git status</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 查看本地仓库的提交历史</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git <span class="built_in">log</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 查看本地仓库的提交历史，简洁版</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git <span class="built_in">log</span> --pretty=oneline</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 查看命令历史</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git reflog</span></span><br></pre></td></tr></table></figure>
<h6 id="分支"><a href="#分支" class="headerlink" title="分支"></a>分支</h6><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 查看分支：</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git branch -a</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 创建本地分支：</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git branch &lt;分支名&gt;</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 切换本地分支：</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git checkout &lt;分支名&gt;</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 创建+切换本地分支：</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git checkout -b &lt;name&gt;</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 合并某分支到当前分支：</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git merge &lt;要合并的分支&gt;</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 将本地分支推送到远程</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git push origin &lt;要推送的本地分支名&gt;</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 以远程分支为基础，建一个本地分支</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git checkout -b &lt;本地分支名&gt; origin/&lt;远程分支名&gt;</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 删除本地分支：</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git branch -d &lt;本地分支名&gt;</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 删除远程分支。将本地空分支推送到远程分支,相当于删除远程分支</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git push origin  :&lt;要删除的远程分支名</span></span><br></pre></td></tr></table></figure>
<h5 id="更新和提交代码"><a href="#更新和提交代码" class="headerlink" title="更新和提交代码"></a>更新和提交代码</h5><p>一个新的文件,或改动.刚开始只存在你的工作区。当你使用git add的时候，Git就会缓存这个改动并且跟踪。当你使用git commit的时候就会把你的改动提交到仓库里。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 缓存所有改动</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git add --all</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 缓存单个文件的改动</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git add &lt;该文件的文件名，包含路径&gt;</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 提交至本地仓库</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git commit -m &lt;提交备注&gt;</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 更新本地代码</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git pull origin &lt;分支名&gt;</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 将本地commit推送至远端</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git push orign &lt;分支名&gt;</span></span><br></pre></td></tr></table></figure>
<h5 id="撤销"><a href="#撤销" class="headerlink" title="撤销"></a>撤销</h5><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 撤销工作区某个文件的更改</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git checkout [file]</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 撤销工作区所有文件的更改</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git checkout .</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 重置暂存区的指定文件，与上一次commit保持一致。但更改并没有消失，而是更改打回工作区</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git reset [file]</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 重置暂存区与工作区，与上一次commit保持一致。</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git reset --hard &lt;当前分支名&gt;</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 重置当前分支的指针为指定commit，同时重置暂存区。但更改并没有消失，而是更改打回工作区</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git reset [commit]  </span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 重置当前分支的HEAD为指定commit，同时重置暂存区和工作区，与指定commit一致。</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git reset --hard [commit]</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 重置当前HEAD为指定commit，但保持暂存区和工作区不变</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git reset --keep [commit]</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 暂时将未提交的变化存入stash，稍后再弹出</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git stash</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git stash pop</span></span><br><span class="line"></span><br><span class="line">git review</span><br></pre></td></tr></table></figure>
<p>代码评审使用gerrit系统，git中使用git review &lt;分支名&gt;(默认是master) 命令执行review操作。</p>
<h5 id="规则"><a href="#规则" class="headerlink" title="规则"></a>规则</h5><ul>
<li>提交reivew之前pull远程代码，保证提交以前是最新代码，有冲突需要本地合并处理。</li>
<li>一个单一的功能的变更放入一个commit中，提交一次reivew。</li>
</ul>
<h5 id="特殊情况"><a href="#特殊情况" class="headerlink" title="特殊情况"></a>特殊情况</h5><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">review没有通过怎么办？先回到要修改的那个commit</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git reset --soft  &lt;要修改的那个commit的id&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">继续修改你要改的文件。修改后add缓存文件，并执行</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git commit --amend</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">将刚生产的变更归并到上一次变更里，继续执行git review</span></span><br><span class="line"><span class="meta">#</span><span class="bash">已经做了多个提交commits怎么办？</span></span><br><span class="line"><span class="meta">#</span><span class="bash">如果多个提交是相关联的，请合并这个提交为一个提交</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 查询最先提交的commit, 记住id.</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git <span class="built_in">log</span> </span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 进行变基操作</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git rebase -i  &lt;上一步查到的id&gt;</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 弹出的界面上罗列了最先提交的commit到现在的所有提交记录</span></span><br><span class="line"><span class="meta">#</span><span class="bash">将每列开头的 <span class="string">'pick'</span> 改成 <span class="string">'s'</span>, 只保留第一列的 <span class="string">'pick'</span>。</span></span><br><span class="line"><span class="meta">#</span><span class="bash">保存修改后系统会自动把这些commits合并成一个commit.</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 如果遇到冲突需要手动解决。合并冲突后，继续变基， 直到所有commits都合并为止.</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git rebase --<span class="built_in">continue</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">如果review中提交了多个commits，其中一个commit没review过怎么办(包括以前某个commit中没有生成change <span class="comment">#id)？一次commit对应生成一个review, 前一个review没通过的话，后面的review 通过了也提交不了。 必须把前面#一个review 弄通过，后面的review才能提交。</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 查询未通过的review对应的commit id(gerrit里有记录)</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 回到这个commit的前一个节点，注意有个^</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> 执行 git rebase -i  &lt;未通过的review对应的commit id&gt;^ </span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 修改并缓存要提交的文件后</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git commit --amend</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 返回head处</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git rebase --<span class="built_in">continue</span> </span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 提交对老review的更新</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git review</span></span><br></pre></td></tr></table></figure>
<h5 id="特别提示"><a href="#特别提示" class="headerlink" title="特别提示"></a>特别提示</h5><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">如果git review &lt;分支名&gt;后提示缺失commit_id，可能是前面rebase操作造成的。</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 现将rebase好的commit推回工作区</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git reset head^</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 再重新add和commit，产生新的commit_id</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git add .</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git commit -m &lt;备注信息&gt;</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> git review &lt;分支名&gt;</span></span><br></pre></td></tr></table></figure>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2019-01-17T06:58:14.112Z" itemprop="dateUpdated">2019-01-17 14:58:14</time>
</span><br>


        
        原文链接：<a href="/2018/07/06/1/" target="_blank" rel="external">https://lvshen9.gitee.io/2018/07/06/1/</a>
        
    </div>
    
    <footer>
        <a href="https://lvshen9.gitee.io">
            <img src="/img/avatar.jpg" alt="我的技术小房间">
            我的技术小房间
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Git/">Git</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/命令/">命令</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lvshen9.gitee.io/2018/07/06/1/&title=《Git命令学习》 — Lvshen's Blog&pic=https://lvshen9.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lvshen9.gitee.io/2018/07/06/1/&title=《Git命令学习》 — Lvshen's Blog&source=Git重要概念master head每次提交，Git都把它们串成一条时间线，这条时间线就是一个分支。在Git里，有个分支叫主分支，即master分支。HE..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lvshen9.gitee.io/2018/07/06/1/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Git命令学习》 — Lvshen's Blog&url=https://lvshen9.gitee.io/2018/07/06/1/&via=https://lvshen9.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lvshen9.gitee.io/2018/07/06/1/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2018/07/20/1/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">我在GitHub上的一个关于Python版打飞机的项目</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2018/07/05/1/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">redis相关命令操作</h4>
      </a>
    </div>
  
</nav>



    











    <!-- Valine Comments -->
    <div class="comments vcomment" id="comments"></div>
    <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
    <script src="//unpkg.com/valine@latest/dist/Valine.min.js"></script>
    <!-- Valine Comments script -->
    <script>
        var GUEST_INFO = ['nick','mail','link'];
        var guest_info = 'nick,mail,link'.split(',').filter(function(item){
          return GUEST_INFO.indexOf(item) > -1
        });
        new Valine({
            el: '#comments',
            notify: 'false' == 'true',
            verify: 'false' == 'true',
            appId: "dy9kXHwg5jQUlLryQmpjWRlM-gzGzoHsz",
            appKey: "P9Nh39Ol0JbMMiYqNGHEP3ml",
            avatar: "mm",
            placeholder: "Just go go",
            guest_info: guest_info.length == 0 ? GUEST_INFO : guest_info,
            pageSize: "10"
        })
    </script>
    <!-- Valine Comments end -->







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="https://lvshen9.github.io/blog2/pay/weixin.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="https://lvshen9.github.io/blog2/pay/weixin.jpg" data-alipay="https://lvshen9.github.io/blog2/pay/zhifu.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
            <span>博客内容遵循 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>我的技术小房间 &copy; 2015 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lvshen9.gitee.io/2018/07/06/1/&title=《Git命令学习》 — Lvshen's Blog&pic=https://lvshen9.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lvshen9.gitee.io/2018/07/06/1/&title=《Git命令学习》 — Lvshen's Blog&source=Git重要概念master head每次提交，Git都把它们串成一条时间线，这条时间线就是一个分支。在Git里，有个分支叫主分支，即master分支。HE..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lvshen9.gitee.io/2018/07/06/1/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Git命令学习》 — Lvshen's Blog&url=https://lvshen9.gitee.io/2018/07/06/1/&via=https://lvshen9.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lvshen9.gitee.io/2018/07/06/1/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=https://lvshen9.gitee.io/2018/07/06/1/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
